/*
Copyright (C) 2003  Pierrick Brihaye
pierrick.brihaye@wanadoo.fr
 
Original Perl code :
Portions (c) 2002 QAMUS LLC (www.qamus.org), 
(c) 2002 Trustees of the University of Pennsylvania 
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the
Free Software Foundation, Inc.
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
or connect to:
http://www.fsf.org/copyleft/gpl.html
*/

package gpl.pierrick.brihaye.aramorph;

import gpl.pierrick.brihaye.util.LRUCache;

import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Logger;

/** An in-memory handler for managing solutions found by the morphological analyzer.
 * @see org.apache.lucene.analysis.ar.aramorph.AraMorph
 *@author Pierrick Brihaye, 2003
 */
public class InMemorySolutionsHandler implements SolutionsHandler{
	
	private static final Logger logger = Logger.getLogger(InMemorySolutionsHandler.class.getName());
	
	/** Solutions for analyzed words.
	 * <PRE>key</PRE> = word
	 * <PRE>value</PRE> = set of solutions (can be empty)
	 */
	private LRUCache<String,Collection<Solution>> solutions;

	/** Alternative spellings for analyzed words.
	 * <PRE>key</PRE> = word
	 * <PRE>value</PRE> = set of alternative spellings (can be empty)
	 */
	private LRUCache<String,Collection<String>> alternativeSpellings;
	
	/** Private constructor to avoid multiple instanciations. */
	public InMemorySolutionsHandler(int size) {
		logger.info("Initializing in-memory solutions handler...");
		solutions = new LRUCache<String, Collection<Solution>>(size);
		alternativeSpellings = new LRUCache<String, Collection<String>>(size);
		logger.info("... done.");
	}
	
	/** Add solutions for the given word.
	 * @param translitered The word
	 * @param sol The solutions
	 */
	public synchronized void addSolutions(String translitered, Collection<Solution> sol) {
		//add it to cache
		solutions.put(translitered, sol);
	}
	
	/** Whether or not the word already gave solutions.
	 * @param translitered The word
	 * @return The result
	 */
	public boolean hasSolutions(String translitered) {
		boolean found = solutions.containsKey(translitered);
		return found;
	}
	
	/** Returns an iterator on the solutions of the given word.
	 * @param translitered The word
	 * @return The iterator
	 */
	public Iterator<Solution> getSolutionsIterator(String translitered) {
		if (!solutions.containsKey(translitered))
			return null;
		
		return solutions.get(translitered).iterator();
	}
	
	/** Add alternative spellings for the given word.
	 * @param translitered The word
	 * @param alt The alternative spellings
	 */
	public synchronized void addAlternativeSpellings(String translitered, Collection<String> alt) {
		//add it to cache
		alternativeSpellings.put(translitered, alt);
	}
	
	/** Whether or not the word already gave alternative spellings.
	 * @param translitered The word
	 * @return The result
	 */
	public boolean hasAlternativeSpellings(String translitered) {
		boolean found = alternativeSpellings.containsKey(translitered);
		return found;
	}
	
	/** Returns an iterator on the alternative spellings of the given word.
	 * @param translitered The word
	 * @return The iterator
	 */
	public Iterator<String> getAlternativeSpellingsIterator(String translitered) {

		if (!alternativeSpellings.containsKey(translitered))
			return null;
		
		return (alternativeSpellings.get(translitered)).iterator();
	}
	
}